from langchain import PromptTemplate, OpenAI, LLMChain

import os

os.environ["OPENAI_API_KEY"] = 'sk-f2M0jWxDEb7IJKiuCnMsvKE7TLSYCA36pyl8gWA1XLaHnD8l'
os.environ["OPENAI_BASE_URL"] = 'https://api.f2gpt.com/v1'

prompt = PromptTemplate(
    input_variables=["flower", "season"],
    template="{flower}在{season}的花语是?",
)

llm = OpenAI(
    model_name="gpt-3.5-turbo-instruct",
    temperature=0)

llm_chain = LLMChain(llm=llm, prompt=prompt)

# 直接调用方式
print(llm_chain({
    'flower': "玫瑰",
    'season': "夏季"}))

# run方法
llm_chain.run({"flower": "玫瑰", "season": "夏季"})

# predict方法
result = llm_chain.predict(flower="玫瑰", season="夏季")
print(result)

# apply方法允许您针对输入列表运行链
input_list = [
    {"flower": "玫瑰", "season": "夏季"},
    {"flower": "百合", "season": "春季"},
    {"flower": "郁金香", "season": "秋季"},
]
result = llm_chain.apply(input_list)
print(result)

# generate方法
result = llm_chain.generate(input_list)
print(result)
